<?php
class CdeUnitsController extends AppController {

	var $name = 'CdeUnits';
	var $helpers = array('Html', 'Form');
	var $uses=array('CdeModule','CdeUnit','CdeUsersUnit','CdeUsersModule');
	/**
	list all units in units table for
	-logged in user
	-each unit has a status
	-completed
	*/
	function viewallunits() {
		$userinformation=parent::beforeFilter();
		$userloggedinid=$userinformation['id'];
		$userloggedinrole=$userinformation['role'];
		$this->CdeUnit->recursive = 0;
		$unitwithstatus=array();
		if ($userloggedinrole=='admin') {
			$tempt=$this->CdeUnit->findAll();
		} else {
			$tempt=$this->CdeUnit->find('all',array('conditions'=>array('CdeUnit.enabled'=>'1'), 'order'=>'CdeUnit.id ASC'));
		}
		//find all units which have enabled
		foreach ($tempt as $unit) {
			//assume userid=1
			$unit['CdeUnit']['status']=$this->CdeUnit->getUnitStatus($unit['CdeUnit']['id'],$userloggedinid); //add status for each unit for logged in user
			array_push($unitwithstatus,$unit);
		}
		$this->set('cdeUnits',$unitwithstatus);
	}
	/**
	 * this function will be called when a unit is selected
	 * current module will be stored and used for the continue button in unit intro view
	 * start module will be stored and used for the start new button in unit intro view
	 *
	 * @param int $id
	 */
	function unitintro($id=null) {
		//get current module from user_module
		//the current module is the module id in user_module by asc
		//if there is no record in the table, set current module = first module
		$userinformation=parent::beforeFilter();
		$userloggedinid=$userinformation['id'];
		$userloggedinrole=$userinformation['role'];
		$status=$this->CdeUnit->getUnitStatus($id, $userloggedinid);//user id
		$selectunit=$this->CdeUnit->findById($id);
		//handle incorrect url entered by users
		if ($userloggedinrole=='admin') {
			$unitexist=$this->CdeUnit->find('all',array('conditions'=>array('CdeUnit.id'=>$id)));
		} else {
			$unitexist=$this->CdeUnit->find('all',array('conditions'=>array('CdeUnit.enabled'=>'1','CdeUnit.id'=>$id), 'order'=>'CdeUnit.id ASC'));
		}
		if ($id == null or empty($unitexist)) {
			$this->redirect(array('action'=>'../cde_units/viewallunits'));
		}

		//check user's role
		//if user's role is admin
		//enable all features
		if ($userloggedinrole=='admin') {
			$firstenabledmodules=$this->CdeModule->find('first',array('conditions'=>array('CdeModule.cde_unit_id'=>$id),'order'=>'CdeModule.id ASC'));
			$lastenabledmodules=$this->CdeModule->find('first',array('conditions'=>array('CdeModule.cde_unit_id'=>$id),'order'=>'CdeModule.id DESC'));
		} else {
			$firstenabledmodules=$this->CdeModule->find('first',array('conditions'=>array('CdeModule.cde_unit_id'=>$id,'CdeModule.enabled'=>'1'), 'order'=>'CdeModule.id ASC'));
			$lastenabledmodules=$this->CdeModule->find('first',array('conditions'=>array('CdeModule.cde_unit_id'=>$id,'CdeModule.enabled'=>'1'), 'order'=>'CdeModule.id DESC'));
		}
		$startmodule=$firstenabledmodules['CdeModule']['id'];
		$lastmodule=$lastenabledmodules['CdeModule']['id'];"";
		$continuemodule=$lastmodule;
		if ($status=='New') {
			$continuemodule=$startmodule;
		}else if($status=='Completed') {
			$continuemodule=$lastmodule;
		}else {//uncompleted
			$lastdonemodule=$this->CdeUsersModule->getContinueModule($id,$userloggedinid);//user id
			if (count($lastdonemodule)>0) {
				$returnContinueModule=$this->CdeModule->getContinueModule($lastdonemodule[0]['cde_users_modules']['cde_module_id'],$id);
				if (count($returnContinueModule)>0) {//next module is the continue module
					$continuemodule=$returnContinueModule[0]['cde_modules']['id'];
				}
			}
		}
		$selectunit['CdeUnit']['startmodule']=$startmodule;
		$selectunit['CdeUnit']['currentmodule']=$continuemodule;
		$this->set('cdeUnit',$selectunit);

	}
}
?>